package de.sordul.jobMaster.util;

import java.util.HashMap;
import java.util.Map;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.matcher.Matchers;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;

import de.sordul.jobMaster.IWordlistDAO;
import de.sordul.jobMaster.JobMasterInterface;
import de.sordul.jobMaster.JobMasterService;
import de.sordul.jobMaster.impl.JobMasterInterfaceREST;
import de.sordul.jobMaster.impl.JobMasterServiceDB;
import de.sordul.jobMaster.impl.WordlistDAO;

/**
 * Guice Servlet configuration file. Replaces web.xml.
 */
public class GuiceServletConfig extends GuiceServletContextListener {

	@Override
	protected Injector getInjector() {
		return Guice.createInjector(new JerseyServletModule() {

            @Override
            protected void configureServlets() {
                
        		// Dependency Injection bindings
        		/////////////////////////////////////////////////////////
        		bind(JobMasterInterface.class).to(JobMasterInterfaceREST.class);
        		bind(JobMasterService.class).to(JobMasterServiceDB.class);
        		bind(IWordlistDAO.class).to(WordlistDAO.class);
        		
        		// Interceptor bindings
        		/////////////////////////////////////////////////////////
        		bindInterceptor(
        			Matchers.any(), 
        			Matchers.annotatedWith(LocalOnly.class), 
        		    new LocalOnlyInterceptor());

        		// JAX-RS Servlet bindings
        		/////////////////////////////////////////////////////////
                bind(JobMasterInterfaceREST.class);
        		
        		// Servlet configuration
        		/////////////////////////////////////////////////////////
                Map<String, String> params = new HashMap<String, String>();
                params.put("com.sun.jersey.config.property.packages","de.sordul.jobMaster");
                params.put("com.sun.jersey.config.feature.DisableWADL","true");
                //params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "unbound");
                serve("/*").with(GuiceContainer.class, params);
            }
		});
	}
}